

export class TimeUtil{

    public static  timeDif(startTime){
        return Date.now() - startTime
    }
    public static  async sleepCore(ms: number): Promise<void> {
        return new Promise(resolve => setTimeout(resolve, ms));
    }




    public static async sleep(duration){
        await this.sleepCore(duration)
    }

    public static getBeijingTime(): string {
        // 获取当前时间
        const beijingTime = Date.now();

       return this.millsTimeStampToDate(beijingTime)
    }
    private static addZero(num: number): string {
        return num < 10 ? `0${num}` : `${num}`;
    }

    public static millsTimeStampToDate(timestamp: number): string {
        // 将 Unix 时间戳转换为 Date 对象
        const date = new Date(timestamp);

        // 创建 DateTimeFormat 对象，并设置时区为中国标准时间（CST）
        const formatter = new Intl.DateTimeFormat('zh-CN', {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit',
            second: '2-digit',
            timeZone: 'Asia/Shanghai',
            hour12: false
        });

        // 格式化时间，并输出
        const parts = formatter.formatToParts(date);
        const formattedDate = parts.map(({ type, value }) => {
            switch (type) {
                case 'year':
                    return `${value}-`;
                case 'month':
                    return `${value}-`;
                case 'day':
                    return `${value} `;
                case 'hour':
                    return `${value}:`;
                case 'minute':
                    return `${value}:`;
                case 'second':
                    return value;
                default:
                    return '';
            }
        }).join('');

        // 获取毫秒部分
        const milliseconds = date.getMilliseconds().toString().padStart(3, '0');

        return `${formattedDate}.${milliseconds}`.trim();
    }

    public static getCurrentExactTime(): string {
        // 创建一个新的Date对象，它将自动设置为当前时间
        const now = new Date();

        // 获取小时、分钟、秒和毫秒
        const hours = now.getHours();
        const minutes = now.getMinutes();
        const seconds = now.getSeconds();
        const milliseconds = now.getMilliseconds();

        // 格式化时间字符串，确保始终有两位数字，毫秒部分保留三位数
        const formattedTime = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}.${milliseconds.toString().padStart(3, '0')}`;

        // 返回格式化的时间字符串
        return formattedTime;
    }







}


